﻿#define _CRT_SECURE_NO_WARNINGS
#include <Windows.h>
#include "chatsetchange.h"
//ASCIIToUnicode
char* str = nullptr;
wchar_t* wstr = nullptr;
char* utf8 = nullptr;  //这些变量的定义用于编码转化
wchar_t* AnsiToUnicode(char* str)
{
    if (wstr)
    {
        delete wstr;
        wstr = NULL;
    }

    int length = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);

    wstr = new wchar_t[length];
    if (wstr == NULL)
        return NULL;

    MultiByteToWideChar(CP_ACP, 0, str, -1, wstr, length);
    return wstr;
}

//UnicodeתUTF-8
char* UnicodeToUtf8(wchar_t* wstr)
{
    if (utf8)
    {
        delete utf8;
        utf8 = NULL;
    }

    int length = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);

    utf8 = new char[length];
    if (utf8 == NULL)
        return NULL;

    WideCharToMultiByte(CP_UTF8, 0, wstr, -1, utf8, length, NULL, NULL);

    return utf8;
}

//ANSIתUTF-8
char* AnsiToUtf8(char* str)
{
    if (wstr)
    {
        delete wstr;
        wstr = NULL;
    }

    if (utf8)
    {
        delete utf8;
        utf8 = NULL;
    }

    //ANSI=>Unicode
    wstr = AnsiToUnicode(str);

    //Unicode=>UTF8
    utf8 = UnicodeToUtf8(wstr);

    return utf8;
}




//UTF-8תUnicode
wchar_t* Utf8ToUnicode(char* utf8)
{
    if (wstr)
    {
        delete wstr;
        wstr = NULL;
    }

    int length = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);

    wstr = new wchar_t[length];
    if (wstr == NULL)
        return NULL;

    MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, length);

    return wstr;
}

//UnicodeתToASCII
char* UnicodeToAnsi(wchar_t* wstr)
{
    if (str)
    {
        delete str;
        str = NULL;
    }

    int length = WideCharToMultiByte(CP_OEMCP, 0, wstr, -1, NULL, 0, NULL, NULL);

    str = new char[length];
    if (str == NULL)
        return NULL;

    WideCharToMultiByte(CP_OEMCP, 0, wstr, -1, str, length, NULL, NULL);
    return str;
}

//UTF-8תANSI
char* Utf8ToAnsi(char* utf8)
{
    if (wstr)
    {
        delete wstr;
        wstr = NULL;
    }

    if (str)
    {
        delete str;
        str = NULL;
    }

    //UTF8=>Unicode
    wstr = Utf8ToUnicode(utf8);

    //Unicode=>ANSI
    str = UnicodeToAnsi(wstr);
    return str;
}


std::string UTF8ToGB(const char* str)
{
    std::string result;
    WCHAR* strSrc;
    LPSTR szRes;

    int i = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
    strSrc = new WCHAR[i + 1];
    MultiByteToWideChar(CP_UTF8, 0, str, -1, strSrc, i);

    i = WideCharToMultiByte(CP_ACP, 0, strSrc, -1, NULL, 0, NULL, NULL);
    szRes = new CHAR[i + 1];
    WideCharToMultiByte(CP_ACP, 0, strSrc, -1, szRes, i, NULL, NULL);

    result = szRes;
    delete[]strSrc;
    delete[]szRes;
    return result;
}

//ASCII转UTF_8
std::string ASCII_To_Utf_8(std::string AsciiStr)
{
    int nStrLen = AsciiStr.size();
    std::string Utf8Str;
    if (nStrLen > 0)
    {
        int nRet(0);
        int nUtf16Len = MultiByteToWideChar(CP_ACP, 0, AsciiStr.c_str(), nStrLen, NULL, 0);
        if (nUtf16Len > 0)
        {
            wchar_t* pW = new wchar_t[nUtf16Len];
            if (pW != nullptr)
            {
                int nUtf8Ret = WideCharToMultiByte(CP_UTF8, 0, pW, nUtf16Len, NULL, 0, NULL, NULL);
                char* pUtf8Buffer = new char[nUtf8Ret + 1];
                if (pW != nullptr)
                {
                    nUtf16Len = MultiByteToWideChar(CP_ACP, 0, AsciiStr.c_str(), nStrLen, pW, nUtf16Len);
                    nRet = WideCharToMultiByte(CP_UTF8, 0, pW, nUtf16Len, pUtf8Buffer, nUtf8Ret, NULL, NULL);
                    pUtf8Buffer[nUtf8Ret] = '\0';
                    Utf8Str.append(pUtf8Buffer, nUtf8Ret + 1);
                    delete[]pUtf8Buffer;
                    pUtf8Buffer = nullptr;
                }
                delete[]pW;
                pW = nullptr;
            }
        }
    }
    return Utf8Str;
}
//UTF_8转ASCII
std::string UTF_8_To_ASCII(std::string& strUtf_8)
{
    std::string strRet("");
    int widesize = ::MultiByteToWideChar(CP_UTF8, 0, strUtf_8.c_str(), -1, NULL, 0);
    if (widesize > 0)
    {
        wchar_t* wBuff = new wchar_t[widesize];
        int convresult = ::MultiByteToWideChar(CP_UTF8, 0, strUtf_8.c_str(), -1, wBuff, widesize);
        int asciisize = ::WideCharToMultiByte(CP_OEMCP, 0, wBuff, -1, NULL, 0, NULL, NULL);
        if (asciisize > 0)
        {
            char* cBuff = new char[asciisize + 1];
            int convresult = ::WideCharToMultiByte(CP_OEMCP, 0, wBuff, -1, cBuff, asciisize, NULL, NULL);
            cBuff[asciisize] = '/0';
            strRet.append(cBuff, asciisize + 1);
            delete[] cBuff;
        }
        delete[] wBuff;
    }
    return strRet;
}
